<?php

declare(strict_types=1);

/**
 * 多行输入框回车换行
 * @param string $str
 * @return string mixed
 */
if (!function_exists('line_feed')) {
    function line_feed(string $str): string
    {
        return str_replace("\n", "<br />", $str ?? '');
    }
}

/**
 * 超出字符省略
 * @param $value
 * @param int $limit
 * @param string $end
 * @return mixed|string
 */
if (!function_exists('str_limit')) {
    function str_limit($value, int $limit = 100, string $end = '...'): mixed
    {
        if (mb_strwidth($value, 'UTF-8') <= $limit) {
            return $value;
        }
        return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')) . $end;
    }
}

/**
 * 获取指定字符之后的数据
 * @param string $str
 * @param string $keyword
 * @return string
 */
if (!function_exists('str_behind')) {
    function str_behind(string $str, string $keyword = '')
    {
        $str = explode($keyword, $str);
        if (count($str) < 1) {
            return $str[0];
        }
        $string = '';
        foreach ($str as $key => $val) {
            if ($key === 0) continue;
            $string .= "/{$val}";
        }
        return $string;
    }
}

/**
 * 格式化
 * @param $time
 * @return string
 */
if (!function_exists('unix_time')) {
    function unix_time($time): string
    {
        //获取今天凌晨的时间戳
        $day = strtotime(date('Y-m-d', time()));
        //获取昨天凌晨的时间戳
        $pday = strtotime(date('Y-m-d', strtotime('-1 day')));
        //获取现在的时间戳
        $nowtime = time();
        $t       = $nowtime - $time;
        if ($time < $pday) {
            $str = date('m-d', $time);
        } elseif ($time < $day && $time > $pday) {
            $str = "昨天";
        } elseif ($t > 60 * 60) {
            $str = floor($t / (60 * 60)) . "小时前";
        } elseif ($t > 60) {
            $str = floor($t / 60) . "分钟前";
        } else {
            $str = "刚刚";
        }
        return $str;
    }
}

/**
 * 二维数组排序
 * @param array $array
 * @param array|string $key
 * @param string $order
 * @return array
 */
if (!function_exists('array_sort')) {
    function array_sort(array $array, array|string $key, string $order = 'asc'): array
    {
        if (is_array($key)){
            foreach ($key as $k=>$v){
                array_multisort(array_column($array, $k), $v == 'desc' ? SORT_DESC : SORT_ASC, $array);
            }
        }else{
            array_multisort(array_column($array, $key), $order == 'desc' ? SORT_DESC : SORT_ASC, $array, $array);
        }
        return $array;
    }
}

/**
 * 毫秒时间戳
 * @return int
 */
if (!function_exists('time_ms')) {
    function time_ms(): int
    {
        [$ms, $sec] = explode(' ', microtime());
        return intval((floatval($ms) + floatval($sec)) * 1000);
    }
}

if (!function_exists('dd')) {
    function dd(...$vars)
    {
        foreach ($vars as $v) {
            var_dump($v);
        }
    }
}